<%@page import="qldc.pm.tiensu.model.TienSu"%>
<%@page import="qldc.pm.tiensu.model.DoiTuongTienSu"%>
<%@page import="qldc.pm.tiensu.constants.TrangThaiTienSuEnum"%>
<%@page import="java.text.Format"%>
<%@page import="qldc.pm.tiensu.constants.TrangThaiTienSuTrongThongKeEnum"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="javax.portlet.ActionResponse"%>
<%@page import="com.liferay.portlet.PortletURLUtil"%>
<%@page import="qldc.pm.tiensu.searchcontainer.DSTienSuSearch"%>
<%@page import="qldc.pm.tiensu.searchcontainer.DSTienSuSearchTerms"%>
<%@page import="qldc.pm.qtht.dmdc.model.Tinh"%>
<%@page import="qldc.pm.qtht.dmdc.service.TinhLocalServiceUtil"%>
<%@page import="java.util.List"%>
<%@page import="qldc.pm.tiensu.service.DoiTuongTienSuLocalServiceUtil"%>
<%@page import="qldc.pm.tiensu.searchcontainer.DSTienSuDisplayTerms"%>
<%@page import="java.util.Date"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="java.text.SimpleDateFormat"%>

<%@include file="../../init.jsp"%>

<%
	PortletURL portletURL = renderResponse.createRenderURL();
	portletURL.setParameter("mvcPath", "/html/view.jsp");
	portletURL.setParameter("chonChucNangItem",	TienSuConstants.DS_BKTT);
	portletURL.setParameter("loaiBKTK",TienSuConstants.DS_TIEN_SU);
	portletURL.setParameter("tabs1",TienSuConstants.DS_TIEN_SU);

	PortletURL thoatUrl =  renderResponse.createRenderURL();
	
	thoatUrl.setParameter("mvcPath", "/html/view.jsp");
	thoatUrl.setParameter("chonChucNangItem",	TienSuConstants.BAO_CAO_THONG_KE);
	
	String pathThemeImages = themeDisplay.getPathThemeImages();
	SimpleDateFormat dateFormat = new SimpleDateFormat(TienSuConstants.DATE_PATTERN);
	
%>

<portlet:resourceURL var="loadQuanHuyen" id="loadQuanHuyen">
	<portlet:param name="tinhIdSelected" value="{0}" />
</portlet:resourceURL>

<portlet:resourceURL var="loadPhuongXa" id="loadPhuongXa">
	<portlet:param name="quanHuyenIdSelected" value="{0}" />
</portlet:resourceURL>

<script type="text/javascript">
var NhapNgay = (function() {
	function NhapNgay() {}
	/*
	 * 
	 */
	 NhapNgay.prototype.initCalendars = function() {
			$("#<portlet:namespace/>tuNgayToaAn").datepicker({
				showOn: "both",
				buttonImage: "<%=pathThemeImages%>/common/calendar.png",
					buttonImageOnly : true,
					buttonText : "<liferay-ui:message key="tuNgayPhamToi" />",
					dateFormat : "dd/mm/yy",
					onClose: function( selectedDate ) {
						 $("#<portlet:namespace/>denNgayToaAn").datepicker( "option", "minDate", selectedDate );
						 }
				});
			$("#<portlet:namespace/>denNgayToaAn").datepicker({
				showOn: "both",
				buttonImage: "<%=pathThemeImages%>/common/calendar.png",
					buttonImageOnly : true,
					buttonText : "<liferay-ui:message key="denNgayPhamToi" />",
					dateFormat : "dd/mm/yy",
					onClose: function( selectedDate ) {
						 $("#<portlet:namespace/>tuNgayToaAn").datepicker( "option", "maxDate", selectedDate );
						 }
				});

		};

		return NhapNgay;
	})();

	$(function() {
		var dom = new NhapNgay();

		dom.initCalendars();
	});
</script>

<portlet:actionURL var="inDanhSachNguoiCoTienSuURL">
	<portlet:param name="<%=TienSuConstants.ACTION_HANDLER_PARAM %>" value="<%=TienSuConstants.REPORT_ACTION %>"/>
	<portlet:param name="<%=Constants.ACTION %>" value="inReportDSNguoiCoTienSu"/>
	<portlet:param name="redirect" value="<%=portletURL.toString() %>"/>
</portlet:actionURL>

<fieldset>
	

<div  class="main-Content" >
	<form action="<%=inDanhSachNguoiCoTienSuURL.toString()%>" id="<portlet:namespace/>frm" method="POST" target="_blank">
			<input type="hidden" name="<portlet:namespace />cmd" id="<portlet:namespace />cmd">
		
		<%
			SimpleDateFormat sdf = new SimpleDateFormat(TienSuConstants.DATE_PATTERN);
			DSTienSuSearch dsTienSuSearch = new DSTienSuSearch(renderRequest,
					portletURL);
			DSTienSuDisplayTerms displayTerms = (DSTienSuDisplayTerms) dsTienSuSearch
					.getDisplayTerms();
			DSTienSuSearchTerms searchTerms = (DSTienSuSearchTerms) dsTienSuSearch
					.getSearchTerms();
			
			long noiThuongTruTinhIdSelected = searchTerms.getTinhId();
			long noiThuongTruQuanHuyenIdSelected = searchTerms.getQuanHuyenId();
			long noiThuongTruPhuongXaIdSelected = searchTerms.getPhuongXaId();
			int totalRow = DoiTuongTienSuLocalServiceUtil.countDsTienSuByVungDiaLy_ThoiGianToaAn
					(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(),
							searchTerms.getTuNgayToaAn(),
							searchTerms.getDenNgayToaAn(),searchTerms.getTrangThai());
		
		%>
		<table class="gridnormal" style="padding-top: 10px;padding-left: 10px">			
			<tr>
				<td class="aui-w20"><liferay-ui:message key="vung-dia-ly" /></td>

				<td colspan="3" class="aui-w80" ><select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSTienSuDisplayTerms.TINH_ID %>" id="<portlet:namespace/>noiThuongTruTinhId"
					onchange="loadQuanHuyen('<portlet:namespace />noiThuongTruTinhId', '<portlet:namespace />noiThuongTruQuanHuyenId', '<portlet:namespace />noiThuongTruPhuongXaId', '<%=loadQuanHuyen.toString()%>','<%=noiThuongTruQuanHuyenIdSelected%>');">
						<option value="0"><liferay-ui:message key="chon-tinh" /></option>
						<%
							List<Tinh> tinhs = TinhLocalServiceUtil.findAll();

							for (Tinh tinh : tinhs) {
						%>
						<option value="<%=tinh.getTinhId()%>" <%=(noiThuongTruTinhIdSelected == tinh.getTinhId() || tinh.getLaMacDinh() ) ? "selected=selected"
						: StringPool.BLANK%>><%=tinh.getTenTinh()%></option>
						<%
							}
						%>
				</select>
				 <select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSTienSuDisplayTerms.QUAN_HUYEN_ID %>" id="<portlet:namespace/>noiThuongTruQuanHuyenId" onchange="loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString()%>', '<%=noiThuongTruPhuongXaIdSelected%>');">
				 	<option value="0"><liferay-ui:message key="chon-quan-huyen" /></option>
				 
				 </select>

				<select class="display-inline" style="width: 30%" name="<portlet:namespace/><%=DSTienSuDisplayTerms.PHUONG_XA_ID %>" id="<portlet:namespace/>noiThuongTruPhuongXaId">
						<option value="0"><liferay-ui:message key="chon-phuong-xa" /></option>				
				</select>
				
				</td>
				
			</tr>
			<tr>
				<td class="txtlabel aui-w20"><liferay-ui:message key="ngay-toa-an-tu" /></td>
				<td class="aui-w40 ip-datepicker"><input type="text" id="<portlet:namespace/>tuNgayToaAn" name="<portlet:namespace/><%=DSTienSuDisplayTerms.TU_NGAY_TOA_AN %>" value="<%=Validator.isNotNull(displayTerms.getTuNgayToaAn())? sdf.format(displayTerms.getTuNgayToaAn()) : ""%>" />
				&nbsp;&nbsp;<liferay-ui:message key="den-ngay" />
				&nbsp;&nbsp;<input type="text" id="<portlet:namespace/>denNgayToaAn" name="<portlet:namespace/><%=DSTienSuDisplayTerms.DEN_NGAY_TOA_AN %>" value="<%=Validator.isNotNull(displayTerms.getDenNgayToaAn())?sdf.format(displayTerms.getDenNgayToaAn()) : ""%>" />
				</td>
				<td class="aui-w40"></td>
				<%-- <td class="aui-w40"><liferay-ui:message key="trang-thai" />&nbsp;&nbsp;
				<%
				TrangThaiTienSuTrongThongKeEnum[] trangThais = TrangThaiTienSuTrongThongKeEnum.values(); 

			%>
				<select style="width:65%" id="<portlet:namespace/>trangThaiXuLyHoSoXoaAnTich" name="<portlet:namespace/><%=DSTienSuDisplayTerms.TRANG_THAI %>" >
								<% for(int i2 =0, l2 = trangThais.length; i2 < l2; i2++){%>
									<option value=<%=trangThais[i2].getValue() %> <%= (trangThais[i2].getValue() == searchTerms.getTrangThai()) ? "selected = 'true'" : ""%>><liferay-ui:message key="<%=trangThais[i2].getKey() %>" /></option>
								<%} %>
				</select>
				</td> --%>
			</tr>
			<tr>
			<td></td>
			<td colspan="3">
			<div align="left">
				<input type=button onclick="timDanhSach();"  id="<portlet:namespace/>Tim"  class="btn btn-primary" name="<portlet:namespace/>Tim" value="<liferay-ui:message key="tim-kiem" />" /> 
			
				<input type=button onclick="taoBaoCao();" <%=totalRow == 0?"disabled='disabled'":"" %>  id="<portlet:namespace/>BaoCao"  class="btn btn-primary" name="<portlet:namespace/>BaoCao" value="<liferay-ui:message key="xuat-bao-cao" />" /> 
			
				<input type=button class="btn"  name="<portlet:namespace/>thoat" value="<liferay-ui:message key="tro-ve" />"  onclick="window.location = '<%=thoatUrl.toString() %>';" />
			</div>
			</td>
			
			</tr>
		</table>
	</form>


<%
	if (searchTerms.getTinhId() > 0){
	%>
<liferay-ui:search-container  emptyResultsMessage="no-web-content-were-found"  searchContainer="<%=dsTienSuSearch %>">
	<liferay-ui:search-container-results>
		<%
			results = 
			DoiTuongTienSuLocalServiceUtil.findDsTienSuByVungDiaLy_ThoiGianToaAn
					(searchTerms.getTinhId(), searchTerms.getQuanHuyenId(),searchTerms.getPhuongXaId(),
							searchTerms.getTuNgayToaAn(),
							searchTerms.getDenNgayToaAn(),TrangThaiTienSuTrongThongKeEnum.DANG_THI_HANH_AN.getValue(), searchContainer.getStart(),
							searchContainer.getEnd(), searchContainer.getOrderByComparator());

			total = totalRow;
			
			pageContext.setAttribute("results", results);
			pageContext.setAttribute("total", total);
		%>
	</liferay-ui:search-container-results>
	
	<%
		int start = dsTienSuSearch.getDelta() * (dsTienSuSearch.getCur() - 1);
	%>
	
	<liferay-ui:search-container-row className="qldc.pm.tiensu.dto.DS_NguoiTienSuDTO" keyProperty="soDinhDanhCaNhan" modelVar="nguoiCoTienSu">
	<%
	String soDinhDanhCaNhan = nguoiCoTienSu.getSoDinhDanhCaNhan();
	List<TienSu> tienSuDangThiHanhAns = DoiTuongTienSuLocalServiceUtil.layDanhSachTienSuDangThiHanhAnCuaDoiTuong(soDinhDanhCaNhan);
	String ngayTienSus = "";
	for(int i=0;i<tienSuDangThiHanhAns.size();i++){
		ngayTienSus+=Validator.isNotNull(tienSuDangThiHanhAns.get(i).getNgayToaAn())? dateFormat.format(tienSuDangThiHanhAns.get(i).getNgayToaAn()):"";
		if (i< tienSuDangThiHanhAns.size() - 1){
			ngayTienSus+=", ";
			
		}
	}
	%>
	
		<liferay-ui:search-container-column-text cssClass="aui-w3 txt-center" name="stt" value="<%=String.valueOf(++start) %>" />
		<liferay-ui:search-container-column-text cssClass="aui-w15" name="ho-va-ten" property="hoTen" />
		<liferay-ui:search-container-column-text cssClass="txt-center" name="so-dinh-danh-ca-nhan" property="soDinhDanhCaNhan" />		
		<liferay-ui:search-container-column-text cssClass="txt-center" name="ngay-sinh">
			<%=Validator.isNotNull(nguoiCoTienSu.getNgaySinh()) ? dateFormat.format(nguoiCoTienSu.getNgaySinh()) :"" %>
		</liferay-ui:search-container-column-text>
		<liferay-ui:search-container-column-text name="dia-chi-thuong-tru" property="diaChi" />
		<liferay-ui:search-container-column-text cssClass="txt-center" name="so-lan-pham-toi" property="soLanPhamToi" />
		<liferay-ui:search-container-column-text cssClass="aui-w8" name="ngay-tien-su" value="<%=ngayTienSus %>"/>
		<liferay-ui:search-container-column-text cssClass="aui-w12" name="trang-thai">
		<liferay-ui:message key="<%=TrangThaiTienSuEnum.getKey(nguoiCoTienSu.getTrangThai()) %>" />
		</liferay-ui:search-container-column-text>

		
	</liferay-ui:search-container-row>

	<liferay-ui:search-iterator searchContainer="<%=dsTienSuSearch %>" />
</liferay-ui:search-container>
<%

	} else {
		%>
		<h5><liferay-ui:message key="vui-long-chon-tieu-chi-lap-bao-cao" /></h5>
	<%}
%>
	</div>
<script type="text/javascript">
$(function() {
	 loadQuanHuyen('<portlet:namespace />noiThuongTruTinhId', '<portlet:namespace />noiThuongTruQuanHuyenId', '<portlet:namespace />noiThuongTruPhuongXaId', '<%=loadQuanHuyen.toString() %>','<%=noiThuongTruQuanHuyenIdSelected %>');
	 loadPhuongXa('<portlet:namespace/>noiThuongTruQuanHuyenId', '<portlet:namespace/>noiThuongTruPhuongXaId', '<%=loadPhuongXa.toString() %>', '<%=noiThuongTruPhuongXaIdSelected %>');
});


function timDanhSach(){
		$('#<portlet:namespace/>frm').removeAttr("target");
		$('#<portlet:namespace/>frm').removeAttr("action");
		$('#<portlet:namespace/>cmd').val('<%=TienSuConstants.TIM%>');
		$("#<portlet:namespace/>frm").attr('onsubmit', 'return true;');
		$("#<portlet:namespace/>frm").submit(); 
		
};
function taoBaoCao(){

		 $('#<portlet:namespace/>cmd').val('<%=TienSuConstants.IN%>');
			$("#<portlet:namespace/>frm").attr('target', '_blank');
			$("#<portlet:namespace/>frm").submit();
	 
	
};
</script>
</fieldset>

